home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr05 / xnot12a.zip / W3EXT.C < prev    next >
C/C++ Source or Header  |  1993-05-20  |  8KB  |  288 lines

  1. #ifdef MSW /* whole file */
  2.  
  3. /* Extended ms window functions - ie drag & drop, clipboard, print.
  4. */
  5. #include "jam.h"
  6. #include "def.h"
  7. #include "time.h" 
  8. #include "malloc.h"
  9. #include "commdlg.h" 
  10. #include "shellapi.h"
  11.  
  12. static char *noclipbtext = "No text on clipboard.";
  13. static char *noopencb = "Can't open clipboard.";
  14. static char *nosetcbtext = "Error setting text to clipboard.";
  15.  
  16. void HandleDroppedFiles(HANDLE hdrop)
  17. {
  18.   char fname[NFILEN+1];
  19.   register UINT numfiles, len, i;
  20.   POINT pt;
  21.   int row, col;
  22.   BOOL insert;
  23.  
  24.   numfiles = (int)DragQueryFile(hdrop, (UINT)-1, fname, NFILEN+1);  
  25.  
  26.   if (numfiles > 0)
  27.     {
  28.       DragQueryPoint(hdrop, &pt);
  29.       GetRowCol(&row, &col, pt.x, pt.y);
  30.       dottomouse(row, col, FALSE);
  31.       if (minmodeline())
  32.         insert = FALSE;            /* find-file-other-window */
  33.       else
  34.         {
  35.           insert = TRUE;        /* insert at dot */
  36.           dottomouse(row, col, TRUE);    /* set doto to mouse pos */
  37.         }
  38.       for (i = 0; i < numfiles; i++)
  39.         {
  40.           len = DragQueryFile(hdrop, (UINT)i, fname, NFILEN+1);
  41.           fname[len] = '\0';
  42.  
  43.           /* Note that both functions execute NOW, placing only
  44.           * parameters (if anything) into the input queue.  This
  45.           * is because this function is occurring as a result of
  46.           * another task being active and no cursor could be up
  47.           * (displayed) when this command executes.  Otherwise, it
  48.           * would have to be deferred via complete input-stream 
  49.           * actions. Note also that this allows many files to be
  50.           * read in without overflowing
  51.           */
  52.           if (insert)
  53.             {
  54.               insertfile(adjustname(fname), (char *)0);
  55.             }
  56.           else
  57.             {
  58.               AddString(fname);
  59.               AddKchar(CCHR('J'));
  60.               poptofilequiet(0, 1);
  61.             }
  62.         }
  63.     AsyncUpdate(); 
  64.     }  
  65. }
  66. void HandleCutCopy(cut)
  67. BOOL cut;
  68. {
  69.   HANDLE handle;
  70.   char *kp, *buffer;
  71.   RSIZE ksize, nlines;
  72.   register RSIZE i, j;
  73.  
  74.   /* nasty to do this here!
  75.   */
  76.   if (IsCaretCreated())
  77.     SetCaretVis(FALSE);
  78.  
  79.   /* either way, data is in our killbuffer
  80.   */
  81.   kdelete();            /* clear the buffer */
  82.   clearUndo(curbp);
  83.   if (cut)
  84.     {
  85.       cacheKill();
  86.       if (!killregion(0, 1))
  87.         {
  88.           WindowMessage("Can't delete region; clipboard not updated.", FALSE);
  89.           clearUndo(curbp);
  90.           return;
  91.         }
  92.     }
  93.   else
  94.     if (!copyregion(0, 1)) 
  95.       {
  96.         WindowMessage("Can't copy region; clipboard not updated.", FALSE);
  97.         return;
  98.       }
  99.  
  100.   /* see if got clipboard
  101.   */
  102.   if (!OpenClipboard(g_hWnd))
  103.     {
  104.       WindowMessage(noopencb, FALSE);
  105.       return;
  106.     }
  107.  
  108.   /* tedious process of figuring out how many
  109.   * lines this buffer has in it
  110.   */
  111.   kp = killbufstart();
  112.   ksize = return_kused();
  113.   nlines = 0;
  114.   for (i = 0; i < ksize; i++)
  115.     if (kp[i] == '\n')
  116.       nlines++;
  117.  
  118.   /* allocate memory for clipboard
  119.   */
  120.   if (!(handle = GlobalAlloc(GMEM_MOVEABLE, ksize + (2 * nlines) + 1)) ||
  121.       !(buffer = (char *)GlobalLock(handle)))
  122.     {
  123.       WindowMessage("Can't get memory to copy data.", FALSE);
  124.       if (handle)
  125.         GlobalFree(handle);
  126.       CloseClipboard();
  127.       return;
  128.     }
  129.   
  130.   /* copy stuff to global mem, adding newline info..
  131.   */
  132.   for (j = i = 0; i < ksize; )
  133.    {
  134.      if (kp[i] == '\n')
  135.        buffer[j++] = '\r';
  136.      buffer[j++] = kp[i++];
  137.      buffer[j] = '\0';            /* always terminate with prejudice */
  138.    }
  139.  
  140.   GlobalUnlock(handle);
  141.   if (!SetClipboardData(CF_TEXT, handle))
  142.     WindowMessage(nosetcbtext, FALSE);
  143.  
  144.   CloseClipboard();
  145.   noop();
  146. }
  147. void HandlePaste()
  148. {
  149.   HANDLE hClipb;
  150.   char *clipdata, *buffer;
  151.   int len;
  152.  
  153.   if (!IsClipboardFormatAvailable(CF_TEXT))
  154.     {
  155.       WindowMessage(noclipbtext, FALSE);
  156.       return;
  157.     }
  158.             
  159.   if (!OpenClipboard(g_hWnd))
  160.     {
  161.       WindowMessage(noopencb, FALSE);
  162.       return;
  163.     }
  164.  
  165.   if (hClipb = GetClipboardData(CF_TEXT))
  166.     {    
  167.       register char *s, *p;
  168.       RSIZE nbytes = 0;
  169.  
  170.       clipdata = GlobalLock(hClipb);
  171.       buffer = malloc((len = strlen(clipdata)) + 1);
  172.       strcpy(buffer, clipdata);
  173.       buffer[len] = '\0';
  174.       GlobalUnlock(hClipb);
  175.  
  176.       clearUndo(curbp);
  177.       for (s = p = buffer; *p; p++)
  178.         if (*p == '\r')
  179.           {
  180.             p += 2;            /* carriage control */
  181.             continue;
  182.           }
  183.         else
  184.           nbytes++;
  185.       cacheInsert(nbytes);
  186.         
  187.       for (s = p = buffer; ; p++)
  188.         if (*p == '\0')
  189.           {
  190.             lineinsert(s, FALSE);    /* add line */
  191.             break;            /* end of buffer */
  192.           }
  193.         else if (*p == '\r')
  194.           {
  195.             *p = '\0';
  196.             p += 2;            /* past ^M and ^J */
  197.             lineinsert(s, TRUE);    /* add line */
  198.             s = p;            /* start new line */
  199.           }
  200.       free(buffer);
  201.       noop();
  202.       /*ExtendedFunction(function_name(refresh));  update */
  203.     }    
  204.   CloseClipboard();
  205. }
  206.  
  207. /* quite simpleminded print function
  208. */
  209. #ifdef DOPRINT
  210. void HandlePrint()
  211. {
  212.   PRINTDLG printdlg;
  213.           
  214.   WindowSleepCursor();
  215.   memset(&printdlg, 0, sizeof(PRINTDLG));
  216.   printdlg.lStructSize = sizeof(PRINTDLG);
  217.   printdlg.hwndOwner = g_hWnd;
  218.   printdlg.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION |
  219.                    PD_HIDEPRINTTOFILE | PD_USEDEVMODECOPIES;
  220.  
  221.   if (PrintDlg(&printdlg) != 0)
  222.     {
  223.       DOCINFO info;
  224.       TEXTMETRIC textmetric;
  225.       int pageht, numlines, lineheight;
  226.  
  227.       /* use default font in hdc...set up lines per page, etc
  228.        */
  229.       GetTextMetrics(printdlg.hDC, &textmetric);
  230.       lineheight = textmetric.tmExternalLeading + textmetric.tmHeight;
  231.       pageht = GetDeviceCaps(printdlg.hDC, VERTRES);
  232.  
  233.       /* start at line 2, stop 2 from bottom, col 2
  234.       */
  235.       numlines = (pageht/lineheight) -4;
  236.       info.cbSize = sizeof(DOCINFO);
  237.       info.lpszDocName = curbp->b_fname;
  238.       info.lpszOutput = NULL;
  239.  
  240.       if (StartDoc(printdlg.hDC, &info) >= 0)
  241.         {
  242.           register int i, j, k;
  243.           register LINE *lp;
  244.           char line[NLINE];
  245.           char c;
  246.  
  247.           for (lp = lforw(curbp->b_linep); lp != curbp->b_linep; )
  248.             {
  249.               StartPage(printdlg.hDC);
  250.               for (i = 2; i < numlines; i++)
  251.                 {
  252.                   /* make an output line; strip control chars, etc
  253.                    */
  254.                   k = 0;
  255.                   line[k] = '\0';
  256.                   for (j = 0; (j < llength(lp)) && (k < NLINE); j++)
  257.                     {
  258.                       c = lgetc(lp, j);
  259.                       if (((c < ' ') || (c > '~')) && (c != '\t'))
  260.                         continue;
  261.                       line[k++] = c;
  262.                       line[k] = '\0';
  263.                     }
  264.  
  265.                   /* start at col 2
  266.                   */
  267.                   TextOut(printdlg.hDC, 2 * textmetric.tmMaxCharWidth, 
  268.                           i * lineheight, line, strlen(line));
  269.                   lp = lforw(lp);
  270.                   if (lp == curbp->b_linep)
  271.                     break;
  272.                 }
  273.               EndPage(printdlg.hDC);
  274.             }
  275.           EndDoc(printdlg.hDC);
  276.         }
  277.       DeleteDC(printdlg.hDC);
  278.       if (printdlg.hDevMode != NULL)
  279.         GlobalFree(printdlg.hDevMode);
  280.       if (printdlg.hDevNames != NULL)
  281.         GlobalFree(printdlg.hDevNames);
  282.     }
  283.   WindowNormalCursor();
  284. }
  285. #endif /* DOPRINT */
  286.  
  287. #endif /* MSW */
  288.